#include "umainwindow.h"
#include <QApplication>
#include <json/json.h>
#include <iostream>

int jsonCpp();

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    UMainWindow w;
    w.show();
    //jsonCpp();
    return a.exec();
}

int jsonCpp()
{
    Json::Value root;
    root["name"] = "吴承恩";
    root["age"] = 30;
    root["city"] = "Wonderland";

    // 创建一个 Json::StreamWriterBuilder 对象，用于配置序列化选项  
    Json::StreamWriterBuilder writer;
    writer["indentation"] = "  "; // 设置缩进为两个空格  

    // 将 JSON 对象序列化为字符串  
    std::string jsonString = Json::writeString(writer, root);
    std::cout << "Serialized JSON:\n" << jsonString << std::endl;

    // 解析 JSON 字符串  
    Json::CharReaderBuilder reader;
    Json::Value parsedRoot;
    std::string errs;
    std::istringstream s(jsonString);

    bool parsingSuccessful = Json::parseFromStream(reader, s, &parsedRoot, &errs);
    if (!parsingSuccessful) {
        std::cerr << "Failed to parse JSON: " << errs << std::endl;
        return 1;
    }

    // 验证解析后的 JSON 对象是否与原始对象相同  
    if (parsedRoot["name"].asString() == "Alice" &&
        parsedRoot["age"].asInt() == 30 &&
        parsedRoot["city"].asString() == "Wonderland") {
        std::cout << "Parsing successful and data matches the original." << std::endl;
    }
    else {
        std::cerr << "Parsed data does not match the original." << std::endl;
    }

    // 输出解析后的 JSON 对象（以美化格式）  
    Json::StreamWriterBuilder writerForOutput;
    writerForOutput["indentation"] = "  ";
    std::string prettyJsonString = Json::writeString(writerForOutput, parsedRoot);
    std::cout << "Parsed and pretty-printed JSON:\n" << prettyJsonString << std::endl;

    return 0;
}